package com.example.sensors;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;

import com.example.singletones.SQLHelper;

public class Sensor_Log_Parser {
	private static JSONObject mConf = null;
	private static int lastLine = 0;
	private final SQLHelper mSQLHelper;
	private SQLiteDatabase db = null;
	private static String PackageLogIntit = "LOG_";
	private static String PackageDBName = "";

	public Sensor_Log_Parser(String LogName) {
		PackageDBName = PackageLogIntit + LogName;
		mSQLHelper = SQLHelper.getInstance();
		db = mSQLHelper.getWritableDatabase();
		String[] SQLArgs = new String[1];
		SQLArgs[0] = PackageDBName;
		Cursor cursor = db.rawQuery(
				"select data from logConfigurations where name=? limit 1",
				SQLArgs);
		while (cursor.moveToNext()) {
			try {
				mConf = new JSONObject(cursor.getString(0));
				if (mConf.has("lastLine")) {
					lastLine = Integer.parseInt((String) mConf.get("lastLine"));
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			mConf = new JSONObject(
					"{\"package\":\"com.waze\",\"regex\":\".*query='q=&.*&longtitude=(\\\\d+(\\\\.\\\\d+)*)&latitude=(\\\\d+(\\\\.\\\\d+)*).*\",\"path\":\"/waze\", \"file\":\"waze_log.txt\", \"logType\":\"file\",\"lastLine\":\"15443\"}");
			String[] SQLArgs3 = new String[2];
			SQLArgs3[0] = mConf.toString();
			SQLArgs3[1] = PackageDBName;
			db.execSQL("update logConfigurations set data=? where name=?",
					SQLArgs3);
			if (mConf != null) {
				parseLog();
				String[] SQLArgs2 = new String[2];
				SQLArgs2[0] = mConf.toString();
				SQLArgs2[1] = PackageDBName;
				db.execSQL("update logConfigurations set data=? where name=?",
						SQLArgs2);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void parseLog() throws JSONException {
		int currentLine = 0;
		if (mConf.has("logType")
				&& (mConf.get("logType").toString().equalsIgnoreCase("file"))) {
			String path = Environment.getExternalStorageDirectory()
					.getAbsolutePath() + mConf.getString("path");
			try {
				File file = new File(path, mConf.getString("file"));
				// FileInputStream iStream = new FileInputStream(path);
				BufferedReader br = new BufferedReader(new FileReader(file));
				String line;
				while ((line = br.readLine()) != null) {
					if (currentLine < lastLine) {
						currentLine++;
					} else {
						if (mConf.has("regex")) {
							Pattern regex = Pattern.compile(
									mConf.getString("regex"), Pattern.DOTALL
											| Pattern.CASE_INSENSITIVE
											| Pattern.UNICODE_CASE
											| Pattern.MULTILINE
											| Pattern.COMMENTS);
							Matcher regexMatcher = regex.matcher(line);
							if (regexMatcher.find()) {
								String ResultString = regexMatcher.group();
								Log.d("test2", ResultString);
							}
						}
						lastLine++;
						currentLine++;
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		mConf.put("lastLine", Integer.toString(lastLine));
	}
}
